<template>
  <div class="about-master-container">
    <base-breadcrumb :breadcrumb="!isAudit">
      <a-form-model
        ref="RuleForm"
        :model="formData"
        :rules="rules"
      >
        <a-card
          v-loading="loading"
          :bordered="false"
        >
          <title-name title="详细信息" />
          <a-row>
            <!-- <a-col span="8">
              <a-form-model-item
                label="所属分公司"
                prop="branchComId"
              >
                <SelectSubCompany
                  ref="selectSubCompany"
                  v-model="formData.branchComId"
                  :disabled="state == 3"
                  placeholder="请选择"
                  @change="getBranchComId"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item
                label="项目名称"
                prop="projectId"
              >
                <SelectProject
                  ref="selectProject"
                  v-model="formData.projectId"
                  :is-clear="id == ''"
                  placeholder="请选择"
                  :form="formData"
                  :disabled="state == 3"
                  key-value="branchComId"
                  @change="getProId"
                />
              </a-form-model-item>
            </a-col> -->
            <a-col span="16">
              <a-form-model-item
                label="所属单位"
                prop="projectId"
              >
                <OrgTreeSelect
                  v-model="formData.projectId"
                  :disabled="state == 3"
                  is-auto-select-first
                  :is-query-projec-detail="false"
                  show-other
                  @select="setProject"
                />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item
                label="表扬单位"
                prop="praiseCompany"
              >
                <BaseInput
                  v-model="formData.praiseCompany"
                  :disabled="state == 3"
                  :max-length="100"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="创建人">
                <BaseInput
                  v-model="formData.createUserName"
                  disabled
                />
              </a-form-model-item>
            </a-col>

            <a-col span="8">
              <a-form-model-item label="创建时间">
                <BaseInput
                  v-model="formData.createTime"
                  disabled
                />
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item
                label="表扬信获得时间"
                prop="gainTime"
              >
                <a-date-picker
                  v-model="formData.gainTime"
                  :disabled="state == 3"
                  valueFormat="YYYY-MM-DD"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item
                label="表扬事项"
                prop="reason"
              >
                <BaseTextarea
                  v-model="formData.reason"
                  :max-length="10000"
                  :disabled="state == 3"
                  placeholder="请输入"
                />
              </a-form-model-item>
            </a-col>
            <a-col
              :md="24"
              :sm="24"
              v-if='state==3'
            >
              <a-form-model-item label="附件上传">
                <file-link :fileList="formData.fileInfoList"></file-link>
              </a-form-model-item>
            </a-col>
            <a-col
              :md="24"
              :sm="24"
              v-else
            >
              <a-form-model-item label="附件上传">
                <upload-list
                  :businessCode="'CUSTOMER05'"
                  :uploadArrys.sync="formData.fileInfoList"
                  @fileArrysData="handleFileUploaded"
                  multiple
                ></upload-list>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-card>
      </a-form-model>

      <div class="pub-header-btn page-btn-right-top">
        <a-button
          v-if="state == 1"
          :loading="loading"
          type="primary"
          @click="onAdd"
        > 提交 </a-button>
        <a-button
          v-if="state == 2"
          :loading="loading"
          type="primary"
          @click="submit"
        > 提交 </a-button>
        <a-button
          v-checkVisibleInDetail
          @click="$close"
        > 关闭 </a-button>
      </div>
    </base-breadcrumb>
  </div>
</template>

<script>
import { planAddFile, planFileList, planFileListDel, planListDetail, print } from '@/api/material/procurement/plan'
import { getCustomerPraiseById, updateByPrimaryPraise, insertSelectivePraise } from '@/api/complaint/satisfactionsurvey.js'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目名称
import UploadList from '@/components/UploadList'
export default {
  name: 'a' + Date.now(),
  props: {
    isAudit: {
      type: Boolean,
      default: false
    }
  },
  components: {
    SelectSubCompany,
    SelectProject,
    UploadList
  },
  data() {
    return {
      key: '1',
      loading: false,
      state: Number(this.$route.query.state),
      isSubmit: false,
      statuCode: '3',
      row: this.$route.query.row,
      formData: {
        projectId: '',
        branchComId: '',
        fileInfoList: [],
        createTime: this.getNowTime(),
        createUserName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name
      },
      rules: {
        projectId: { required: true, message: '请选择项目名称', trigger: 'change' },
        branchComId: { required: true, message: '请选择所属分公司', trigger: 'change' },
        praiseCompany: { required: true, message: '请输入表扬单位', trigger: 'blur' },
        reason: { required: true, message: '请输入表扬事项', trigger: 'blur' },
        gainTime: { required: true, message: '请选择表扬信获得时间', trigger: 'change' }
      },
      mode: this.$route.query.pageTyp === 'add' ? 'add' : 'edit',
      projectLoading: false,
      tsUserOptions: [],
      sgOptions: [],
      planAddFile: planAddFile(),
      planFileList: planFileList(),
      planFileListDel: planFileListDel(),
      getProgrammeArr: [],
      bimData: [],
      proData: [],
      id: '',
      subcontractingData: [],
      treeLoading: false,
      yhData: [],
      typeData: [],
      projectData: [],
      ownerData: [],
      treeData: []
    }
  },

  watch: {
    formData: {
      deep: true,
      handler() {
        this.$checkRequireItem()
      }
    },
    isAudit: {
      // 是否作为审批详情
      immediate: true,
      handler(val) {
        if (val) {
          this.state = 3
        }
      }
    }
  },
  created() {},
  mounted: function () {
    if (this.state != 1) {
      this.id = this.$route.query.id
      this.getDetail()
      this.isSubmit = true
    }
  },
  methods: {
    handleFileUploaded(list) {
      this.formData.fileInfoList = [...list]
    },
    /**
     * @description: 设置分公司和项目信息
     * @param {object} val 选中数据
     * @return {void}
     */
    setProject(val) {
      if (val && val.length > 0) {
        this.formData.branchComId = val[0]?.orgId
        this.formData.branchComName = val[0]?.orgName
        this.formData.projectName = val[1]?.projectName
      }
    },
    // getBranchComId(value) {
    //   const data = this.$refs.selectSubCompany.getValueName(value)
    //   if (data) {
    //     this.formData.branchComName = data
    //   }
    // },
    // getProId(value) {
    //   const projectName = this.$refs.selectProject.getValueName(value)
    //   if (projectName) {
    //     this.formData.projectName = projectName
    //   }
    // },
    getTabChange(e) {
      this.$checkRequireItem()
    },
    getDetail() {
      this.loading = true
      getCustomerPraiseById({ id: this.$route.query.id }).then((res) => {
        this.formData = res.data
        this.loading = false
      })
    },
    onAdd() {
      this.$refs.RuleForm.validate((valid) => {
        if (valid) {
          if (!this.formData.fileInfoList.length) {
            this.$message.warning('请上传附件')
            return
          }
          this.$submitConfirm(async () => {
            this.loading = true
            insertSelectivePraise({ ...this.formData }).then((res) => {
              if (res.code == 200) {
                this.loading = false
                this.$message.success('提交成功')
                this.$multiTab.close(this.$route.fullpath)
              } else {
                this.loading = false
              }
            })
          })
        } else {
          this.$message.warning("请填写完所有必填项信息")
        }
      })
    },
    submit() {
      this.$refs.RuleForm.validate((valid) => {
        console.log(valid, 'valid')
        if (valid) {
          if (!this.formData.fileInfoList || !this.formData.fileInfoList.length) {
            this.$message.warning('请上传附件')
            return
          }
          this.$submitConfirm(async () => {
            this.loading = true
            updateByPrimaryPraise({ ...this.formData }).then((res) => {
              if (res.code == 200) {
                this.loading = false
                this.$message.success('提交成功')
                this.$multiTab.close(this.$route.fullpath)
              } else {
                this.loading = false
              }
            })
          })
        } else {
          this.$message.warning("请填写完所有必填项信息")
        }
      })
    }
  }
}
</script>
